from app.lib import MysqlPool
import env_config
from app.utils.util import calcCrfuid
from app.utils.crypt import des_ecb_encode,des_ecb_decode

def query_customer_basic_info(data):
    print(data)
    dbinfo = env_config.ENV_LIST.get(data.get('env_tools')).get('crf_crm')
    dbtool = MysqlPool.MysqlPool(dbinfo.get('db_name'))
    if data.get('crfuid'):
        data['table_prefix'] =calcCrfuid(data.get('crfuid'))
        user =dbtool.getOne("select * from {}.crf_customer_basic_info_{} where (tel_no=%s or id_no=%s or crfuid=%s or name=%s) and  crfuid<>'' order by create_time desc"
                            .format(dbinfo.get('table_name'),data.get('table_prefix')),(des_ecb_encode(data.get('phone'),'YSIa0qCTPVrTL5NW3ulHq4CJ'),des_ecb_encode(data.get('idno'),'YSIa0qCTPVrTL5NW3ulHq4CJ'),data.get('crfuid'),data.get('name')))
    else:
        for x in range(0,10):
            user = dbtool.getOne(
                "select * from {}.crf_customer_basic_info_{} where (tel_no=%s or id_no=%s or crfuid=%s or name=%s) and  crfuid<>'' order by create_time desc"
                .format(dbinfo.get('table_name'), str(x)),
                (des_ecb_encode(data.get('phone'), 'YSIa0qCTPVrTL5NW3ulHq4CJ'),
                 des_ecb_encode(data.get('idno'), 'YSIa0qCTPVrTL5NW3ulHq4CJ'),
                 data.get('crfuid'),
                 data.get('name')))
            if user:
                break
    if user == False:
        return False
    data['phone'] = des_ecb_decode(user.get('tel_no'), 'YSIa0qCTPVrTL5NW3ulHq4CJ')
    data['idno'] = des_ecb_decode(user.get('id_no'), 'YSIa0qCTPVrTL5NW3ulHq4CJ')
    data['crfuid'] = user.get('crfuid')
    data['name'] = user.get('name')
    data['table_prefix'] = calcCrfuid(user.get('crfuid'))
    data['crf_customer_basic_info'] = user
    return True

def query_customer_limit(data):
    if data.get('crfuid') is None or data.get('crfuid')=='':
        return False
    dbinfo = env_config.ENV_LIST.get(data.get('env_tools')).get('crf_crm')
    dbtool = MysqlPool.MysqlPool(dbinfo.get('db_name'))
    if data.get('table_prefix') is None or data.get('table_prefix')=='':
        data['table_prefix'] = calcCrfuid(data.get('crfuid'))
    result = dbtool.getAll(
        "select * from {}.crf_limit_{} where crfuid=%s"
        .format(dbinfo.get('table_name'), data.get('table_prefix')), (data.get('crfuid')))
    if result == False:
        return False
    data['crf_limit'] = result
    result = dbtool.getAll(
        "select * from {}.crf_limit_detail_{} where crfuid=%s"
            .format(dbinfo.get('table_name'), data.get('table_prefix')), (data.get('crfuid')))
    if result == False:
        return False
    data['crf_limit_detail'] = result
    return True

#查询消费贷会员
def query_member_xfd(data):
    if data.get('crfuid') is None or data.get('crfuid')=='':
        return False
    dbinfo = env_config.ENV_LIST.get(data.get('env_tools')).get('crf_crm')
    dbtool = MysqlPool.MysqlPool(dbinfo.get('db_name'))
    if data.get('table_prefix') is None or data.get('table_prefix')=='':
        data['table_prefix'] = calcCrfuid(data.get('crfuid'))
    print(data)
    result = dbtool.getOne(
        "select * from {}.crf_member_info_{} where crf_uid=%s"
        .format(dbinfo.get('table_name'), data.get('table_prefix')), (data.get('crfuid')))
    if result == False:
        return False
    data['crf_member_info_xfd'] = result
    return True

#查询信富期贷会员
def query_member_xfqd(data):
    if data.get('crfuid') is None or data.get('crfuid')=='':
        return False
    dbinfo = env_config.ENV_LIST.get(data.get('env_tools')).get('crf_crm')
    dbtool = MysqlPool.MysqlPool(dbinfo.get('db_name'))
    print(data)
    result = dbtool.getOne(
        "select * from {}.crf_qshd_member_info where crf_uid=%s"
        .format(dbinfo.get('table_name')), (data.get('crfuid')))
    if result == False:
        return False
    data['crf_qshd_member_info'] = result
    return True

if __name__=='__main__':
    data={}
    data['phone'] = '17621138137'
    data['env'] = 'UAT'
    print(data)